
<template>
    <div class="container">
        <div class="filter-bar">
            <el-select v-model="filter.groupId" placeholder="请选择分组" style="width: 120px;">
                <el-option v-for="item in groupOptions" :key="item.id" :label="item.groupName" :value="item.id" />
            </el-select>
            <el-select v-model="filter.lottery" placeholder="请选择彩种" style="width: 120px;">
                <el-option v-for="item in lotteryOptions" :key="item.id" :label="item.lotteryName" :value="item.lottery" />
            </el-select>
            <el-input v-model.number="filter.qnum" type="number" style="width: 80px; margin-right: 12px;" />
            <el-button type="primary" @click="onSearch">查 询</el-button>
        </div>
        <el-table :data="tableData" border stripe class="stat-table" style="margin-top: 16px;" height="calc(100vh - 180px)">
            <el-table-column prop="name" label="号码名称" min-width="120" align="center" />
            <el-table-column prop="forget" label="当前遗漏" min-width="100" align="center" sortable />
            <el-table-column prop="lastForget" label="上次遗漏" min-width="100" align="center" sortable />
            <el-table-column prop="maxForget" label="最大遗漏" min-width="100" align="center" sortable />
            <el-table-column prop="diff" label="差值" min-width="80" align="center" sortable />
            <el-table-column label="操作" min-width="80" align="center">
                <template #default="scope">
                    <el-button size="small" type="primary" @click="onDetail(scope.row)">查看</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog v-model="viewNumberDialogVisible" title="查看号码" width="400px" class="number-dialog">
            <div class="enhanced-toolbar">
                <el-button size="small" @click="copyNumbers" style="margin-left: 8px;">复制</el-button>
            </div>
            <div class="number-list beautified-list">
                <template v-for="(row, idx) in formattedNumbers" :key="idx">
                    <div class="number-row">{{ row }}</div>
                </template>
            </div>
        </el-dialog>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

import { getGroup } from '@/api/Omission/group'
import { getLottery } from '@/api/Omission/maintain'
import { calcOmission } from '@/api/Omission/statistics'
import { getNumbers } from "@/api/Omission/number";

const groupOptions = ref([])
const lotteryOptions = ref([])

const init = async () => {
    const res1 = await getGroup()
    groupOptions.value = res1.data
    const res2 = await getLottery()
    lotteryOptions.value = res2.data
}
init()

const filter = reactive({
    groupId: "",
    lottery: "",
    qnum: 100
})

const tableData = ref([])

const onSearch = async () => {
    if (!filter.groupId) {
        ElMessage.warning("请选择分组");
        return
    }
    if (!filter.lottery) {
        ElMessage.warning("请选择彩种");
        return
    }
    if (!filter.qnum || filter.qnum <= 0 || filter.qnum > 2000) {
        ElMessage.warning("请输入正确的期数");
        return
    }
    const res = await calcOmission({
        groupId: filter.groupId,
        lottery: filter.lottery,
        qnum: filter.qnum
    });
    if (res.code === "S") {
        tableData.value = res.data || []
    } else {
        ElMessage.error(res.message || "查询失败")
    }
}

const numberString = ref('')
const formattedNumbers = ref([])
const viewNumberDialogVisible = ref(false)
const onDetail = async (row) => {
    console.log('查看号码', row)
    const params = { id: row.context }
    const res = await getNumbers(params)
    if (res.code === 'S') {
        // 假设res.data.numbers为字符串
        numberString.value = res.data.numbers || ''
        formattedNumbers.value = numberString.value.split(' ').reduce((acc, cur, idx) => {
            if (idx % 5 === 0) acc.push([])
            acc[acc.length - 1].push(cur)
            return acc
        }, []).map(arr => arr.join(' '))
        viewNumberDialogVisible.value = true
    } else {
        ElMessage.error(res.message || '获取号码详情失败')
    }
}

const copyNumbers = async () => {
    try {
        await navigator.clipboard.writeText(numberString.value)
        ElMessage.success('已复制到剪贴板')
    } catch {
        ElMessage.error('复制失败')
    }
}
</script>

<style scoped lang="scss">
@use "@/assets/global.scss" as *;

.filter-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}
.stat-table {
    background: #fcfcfc;
    border-radius: 6px;
    box-shadow: 0 2px 8px 0 rgba(0,0,0,0.03);
}

.number-dialog {
    --el-dialog-border-radius: 12px;
}

.enhanced-toolbar {
    gap: 8px;
    background: #f7f8fa;
    border-radius: 8px;
    padding: 6px 10px 6px 0;
    box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.04);
}

.number-list {
    max-height: 300px;
    overflow-y: auto;
    font-family: monospace;
    font-size: 15px;
    line-height: 1.8;
    word-break: break-all;
    background: #f9fafb;
    border-radius: 8px;
    padding: 10px 12px;
    box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.03);
}

.beautified-list .number-row {
    padding: 2px 0 2px 4px;
    border-bottom: 1px dashed #e5e6eb;
    transition: background 0.2s;
}

.beautified-list .number-row:last-child {
    border-bottom: none;
}
</style>